home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MACD 5
/
MACD 5.bin
/
workbench
/
datatypes
/
playdt13.lha
/
PlayDT
/
PlayDT-src
/
PlayFile.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-06-05
|
5KB
|
161 lines
/****************************************************************
*
* $VER: PlayFile.c 1.3 (4.6.97) Tak Tang (tst92@ecs.soton.ac.uk)
*
* Copyright © 1997 Tak Tang
*
* You may use any part of this source code in your own programs
* provided that it is not called PlayDT.
*
*****************************************************************/
/**** Header files ****/
#include "PlayDT.h"
#include <exec/execbase.h>
#include <datatypes/datatypes.h>
#include <datatypes/datatypesclass.h>
#include <datatypes/soundclass.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/datatypes_protos.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/datatypes_pragmas.h>
/****** PlayFile ***************************************************
*
* NAME
* PlayFile -- Play a sample using the sound datatype.
*
* SYNOPSIS
* error = PlayFile( GlobalData, FileName )
*
* ULONG = PlayFile( struct tGlobalData *, STRPTR );
*
* FUNCTION
* This routine plays a single file using the sound datatype.
* If the procedure receives a CTRL-C or a CTRL-D, it will halt
* the current sound. In addition, a CTRL-C will set the
* gd->UserStop flag, to indicate that no further samples should
* be played.
*
* INPUTS
* GlobalData - A pointer to structure containing library bases
* and other global data.
*
* FileName - The filename of the sound sample to play, relative
* to the current directory. This is not checked
* to see if it is a valid filename.
*
* RESULT
* error - ) if NewDTObject was OK, 1 otherwise
* (gd->UserStop) - This flag is set if the sound playback was
* interrupted by a CTRL-C
*
* EXAMPLE
*
* NOTES
* Errors in NewDTObject are now reported back to calling function.
*
* There are two methods to wait for the sound to finish. The 3.0
* sound datatype requires that we calculate the duration of the
* sample, and do a delay. This function checks for CTRL-C and
* CTRL-D each second. A better way to do this would be to set up
* a timer request and doing a wait(), but I need to research this.
* The KS3.1 and Jonathan Gapen's sound datatypes can signal the
* process when the sound has finished playing.
*
* BUGS
*
* SEE ALSO
* sound.datatype, alib/DoMethod()
*
*****************************************************************************
*
*/
ULONG PlayFile(struct GlobalData *gd, STRPTR file)
{
Object *o;
ULONG signal;
ULONG rc=1;
o = NewDTObject ( (APTR)file,
DTA_SourceType, DTST_FILE,
DTA_GroupID, GID_SOUND,
SDTA_Volume, 64,
SDTA_Cycles, 1,
TAG_DONE);
if ( NULL != o )
{
/* Clear signals */
SetSignal(0L, SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D);
/* Attempt to use KS3.1 signalling */
if ( 2 == SetDTAttrs(o, NULL, NULL,
SDTA_SignalTask, gd->MeTask,
SDTA_SignalBit, (ULONG) SIGBREAKF_CTRL_D,
TAG_END) )
{
/* Play the sound using KS3.1 signal */
DoDTMethod(o, NULL, NULL, DTM_TRIGGER, NULL, STM_PLAY, NULL);
signal=Wait (SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D);
} /* if can use KS3.1 signal */
else
{
ULONG length = 0, period = 394, cycles = 1, ticks;
/* Obtain information from the object. */
GetDTAttrs(o, SDTA_SampleLength, &length,
SDTA_Period, &period, SDTA_Cycles, &cycles,
TAG_END);
/* Calculate duration of the sound. */
ticks = 1 + ( length * cycles * period ) /
(gd->SysBase->ex_EClockFrequency * 5);
/* Start the sound. */
DoDTMethod(o, NULL, NULL, DTM_TRIGGER, NULL, STM_PLAY, NULL);
/* Play, checking each second if user tried to abort */
/* The proper way to do this, of course, is to set up a
timer to signal in ticks seconds, then wait. If the
user aborts early, then cancel the timer.
*/
for ( signal=0; ticks && (0==signal) ; ticks --)
{
Delay(50);
signal=SetSignal(0L, 0L) & (SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D) ;
}
} /* if cannot use KS3.1 signal */
/* if user sent a CTRL-C, let calling function know */
if (signal & SIGBREAKF_CTRL_C)
{
gd->UserStop=TRUE;
} /* if user pressed CTRL-C */
/* Get rid of the object */
DisposeDTObject (o);
/* Playing succedded, right? */
rc=0;
} /* if NewDTObject() ok */
return rc;
} /* PlayFile() */
/**** End of file ****/